/**
 * Created by Wind on 2017/10/13.
 */
/**
 * List of tab names.
 * @private
 */


/**
 * Initialize Blockly.  Called on page load.
 */
var workspace = null;

function start() {

    // Setup blocks
    // Parse the URL arguments.
    var match = location.search.match(/dir=([^&]+)/);
    var rtl = match && match[1] == 'rtl';
    //document.forms.options.elements.dir.selectedIndex = Number(rtl);
    var toolbox = toolboxXml();
    //document.forms.options.elements.toolbox.selectedIndex =
    //toolbox ? 1: 0;

    match = location.search.match(/side=([^&]+)/);

    var side = match ? match[1] : 'start';

    //document.forms.options.elements.side.value = side;

    //Create main workspace.
    workspace = Blockly.inject('content_blocks', {
        comments: false,
        disable: false,
        collapse: false,
        media: '/static/blockly/media/',
        readOnly: false,
        rtl: rtl,
        scrollbars: true,
        toolbox: toolbox,
        toolboxPosition: side == 'top' || side == 'start' ? 'start' : 'end',
        horizontalLayout: side == 'top' || side == 'bottom',
        //sounds: soundsEnabled,
        grid:
            {
                spacing: 20,
                length: 3,
                colour: '#ccc',
                snap: true
            },
        zoom: {
            controls: true,
            wheel: true,
            startScale: 0.72,
            maxScale: 4,
            minScale: 0.25,
            scaleSpeed: 1.1
        },
        colours: {
            fieldShadow: 'rgba(255, 255, 255, 10)',
            dragShadowOpacity: 0.6
        },
        trashcan: true
    });

    var loadInput = document.getElementById('load');
    loadInput.addEventListener('change', load, false);
    document.getElementById('fakeload').onclick = function () {
        loadInput.click();
    };
    workspace.addChangeListener(block_defind_event);
}


block_field_map={
    "inout_digital_pin":"d_pin_values",
    "inout_analog_pin":"a_pin_values",
    "customs_motor_m":"text_values"
    };


const_block_field_map={
    "green_servo_move":7,
    "white_servo_move":11,
    "blue_servo_move":8,
    "black_servo_move":13,
    "red_servo_move":4,
    "customs_servo_motor_two":"MT"
};

function check_include(item) {
    if(item in block_field_map){
        return true
    }else{
        return false
    }
}

function const_check_include(item) {
    if(item in const_block_field_map){
        return true
    }else{
        return false
    }
}

function get_field_value(key) {
    return block_field_map[key]
}


function get_const_field_value(key) {
    return const_block_field_map[key]
}

function block_defind_event(event) {
    if(event.type==Blockly.Events.BLOCK_CHANGE){
        var old_value=event.oldValue;
        var new_value=event.newValue;
        var block_obj=workspace.getBlockById(event.blockId);
        if(check_include(block_obj.type)){
            window.online_data_obj.remove_pin_mode(event.blockId);
            window.online_data_obj.set_pin_mode(event.blockId,new_value);
            // console.info(window.online_data_obj.get_pin_mode_map())

        }
    }
    if(event.type==Blockly.Events.BLOCK_CREATE){
        // var block_obj=workspace.getBlockById(event.blockId);
        // var block_name=block_obj.type;
        // for(var i=0;i<block_obj.childBlocks_.length;i++){
        //     if(check_include(block_obj.childBlocks_[i].type)){
        //         var pin=block_obj.childBlocks_[i].getFieldValue(get_field_value(block_obj.childBlocks_[i].type));
        //         window.online_data_obj.set_pin_mode(block_obj.childBlocks_[i].id,pin);
        //         // console.info(window.online_data_obj.get_pin_mode_map())
        //     }
        // }
        //
        // if(const_check_include(block_obj.type)){
        //         window.online_data_obj.set_pin_mode(block_obj.id,get_const_field_value(block_obj.type));
        //         // console.info(window.online_data_obj.get_pin_mode_map())
        // }
        var all_blocks=workspace.getAllBlocks();
        for(var i=0;i<all_blocks.length;i++) {
            if (check_include(all_blocks[i].type)) {
                var pin = all_blocks[i].getFieldValue(get_field_value(all_blocks[i].type));
                window.online_data_obj.set_pin_mode(all_blocks[i].id, pin);
                // console.info(window.online_data_obj.get_pin_mode_map())
            }
            if (const_check_include(all_blocks[i].type)) {
                window.online_data_obj.set_pin_mode(all_blocks[i].id, get_const_field_value(all_blocks[i].type));
                // console.info(window.online_data_obj.get_pin_mode_map())
            }
        }

    }
    if(event.type==Blockly.Events.BLOCK_DELETE){
        var ids=event.ids;
        for(var i=0;i<ids.length;i++){
                window.online_data_obj.remove_pin_mode(ids[i]);
                // console.info(window.online_data_obj.get_pin_mode_map())
        }
    }

     if(window.localStorage && (window.localStorage.setItem('a', 123) , window.localStorage.getItem('a') == 123)){
            //判断浏览器是否支持localStorage
            var xmlDom = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
            var xmlText = Blockly.Xml.domToPrettyText(xmlDom);
            localStorage.setItem('block_xml',xmlText);
        }
}





